package com.directions.android.screens;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import com.directions.android.DialogUtils;
import com.directions.android.DialogUtils.TextInputListener;
import com.directions.android.R;

public class PreferenceFragment extends Fragment {

	public static final String DELETE_LOCATION_HISTORY_SEPARATOR = "_";
	/**
	 * Keeps track of the previous location that the user entered in the menu
	 * text fields.
	 * 
	 * <p>
	 * The format is:
	 * </p>
	 * <p>
	 * LocationName1<strong>{@value #DELETE_LOCATION_HISTORY_SEPARATOR}
	 * </strong>LocationName2<strong>{@value #DELETE_LOCATION_HISTORY_SEPARATOR}
	 * </strong> LocationName3<strong>
	 * {@value #DELETE_LOCATION_HISTORY_SEPARATOR}</strong>...<strong>
	 * {@value #DELETE_LOCATION_HISTORY_SEPARATOR}</strong>LocationNameN
	 * </p>
	 * {@value #DELETE_LOCATION_HISTORY_SEPARATOR} could be considered
	 * separators.
	 */
	public static final String DELETE_LOCATION_HISTORY = "del_hist";
	public static final String HOMEPAGE_IS_MENU = "homepage";
	public static final String REGION_BIAS = "reg_Bias";
	public static final String MARKERS = "markers";

	private View rootView;
	private RadioGroup rgInitialScreen;

	public static Fragment getInstance() {
		Fragment menuFragment = new PreferenceFragment();
		return menuFragment;
	}

	public PreferenceFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		rootView = inflater.inflate(R.layout.preferences, container, false);

		// Init RadioGroup Widget GUI
		rgInitialScreen = (RadioGroup) rootView
				.findViewById(R.id.radioHomepage);

		final SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(getActivity());
		boolean showMarkers = sp.getBoolean(MARKERS, true);
		boolean menuIsHomepage = sp.getBoolean(HOMEPAGE_IS_MENU, true);
		boolean displayRegionBias = sp.getBoolean(REGION_BIAS, true);
		rgInitialScreen
				.check(menuIsHomepage ? R.id.radioSearch : R.id.radioMap);
		final CheckedTextView region_bias = (CheckedTextView) rootView
				.findViewById(R.id.ctvSug);
		region_bias.setChecked(displayRegionBias);
		final CheckedTextView my_places = (CheckedTextView) rootView
				.findViewById(R.id.ctvMark);
		my_places.setChecked(showMarkers);
		rgInitialScreen
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup arg0, int id) {
						RadioButton selectRadio = (RadioButton) rootView
								.findViewById(id);

						sp.edit()
								.putBoolean(
										HOMEPAGE_IS_MENU,
										selectRadio
												.getText()
												.toString()
												.equals(getActivity()
														.getString(
																R.string.search)))
								.apply();
					}

				});

		addListenerOnButton();

		region_bias.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				boolean checked = !region_bias.isChecked();
				sp.edit().putBoolean(REGION_BIAS, checked).apply();

				region_bias.setChecked(checked);

			}
		});

		my_places.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				boolean checked = !my_places.isChecked();
				sp.edit().putBoolean(MARKERS, checked).apply();

				my_places.setChecked(checked);

			}
		});
		return rootView;
	}

	public void addListenerOnButton() {

		Button button = (Button) rootView.findViewById(R.id.deleteHistory);

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				dialog(v);
			}
		});
	}

	private void dialog(View view) {

		DialogUtils.showMessageDialog(getActivity(),
				getActivity().getString(R.string.deleteHistory), getActivity()
						.getString(R.string.areyousure),
				new TextInputListener() {

					@Override
					public void input(String msg) {
						SharedPreferences sp = PreferenceManager
								.getDefaultSharedPreferences(getActivity());
						sp.edit()
								.putString(DELETE_LOCATION_HISTORY,
										"Mi posición actual"
								/* getString(R.string.my_location) */).apply();
					}

					@Override
					public void canceled() {
					}

				});

	}
}
